package com.gmics.unifiq;

import android.app.Activity;
import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.NfcAdapter.CreateNdefMessageCallback;
import android.nfc.NfcAdapter.OnNdefPushCompleteCallback;
import android.nfc.NfcEvent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.provider.Settings;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.gmics.android.R;
import com.gmics.unifiq.controller.EntityController;
import com.gmics.unifiq.controller.UserFunctions;
import com.gmics.unifiq.entity.Globals;
import com.gmics.unifiq.entity.User;

public class FindFriend extends Activity implements CreateNdefMessageCallback,
		OnNdefPushCompleteCallback {
	NfcAdapter mNfcAdapter;
	TextView text, name, email, birthday, school, emailLab, birthdayLab, schoolLab;
	ProgressBar progBar;
	ImageView profilePic;
	Intent i;
	String uid;
	String friend;
	User user = new User();
	EntityController ec = new EntityController();
	
	
	private static final int MESSAGE_SENT = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_find_friend);

		text = (TextView) findViewById(R.id.text);
		name = (TextView) findViewById(R.id.name);
		email = (TextView) findViewById(R.id.email);
		birthday = (TextView) findViewById(R.id.birthday);
		school = (TextView) findViewById(R.id.school);
		emailLab = (TextView) findViewById(R.id.emailLab);
		birthdayLab = (TextView) findViewById(R.id.birthdayLab);
		schoolLab = (TextView) findViewById(R.id.schoolLab);
		progBar = (ProgressBar) findViewById(R.id.progBar);
		profilePic = (ImageView) findViewById(R.id.profilePic);

		// Check for available NFC Adapter
		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
		if (mNfcAdapter == null) {
			text = (TextView) findViewById(R.id.text);
			text.setText("NFC is not available on this device.");
		} else {
			// Register callback to set NDEF message
			mNfcAdapter.setNdefPushMessageCallback(this, this);
			// Register callback to listen for message-sent success
			mNfcAdapter.setOnNdefPushCompleteCallback(this, this);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.

		// If NFC is not available, we won't be needing this menu
		if (mNfcAdapter == null) {
			return super.onCreateOptionsMenu(menu);
		}

		getMenuInflater().inflate(R.menu.find_friend, menu);
		return true;
	}

	@Override
	public void onNdefPushComplete(NfcEvent arg0) {

		// A handler is needed to send messages to the activity when this
		// callback occurs, because it happens from a binder thread
		mHandler.obtainMessage(MESSAGE_SENT).sendToTarget();
		
	}

	/** This handler receives a message from onNdefPushComplete */
	private final Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			switch (msg.what) {
			case MESSAGE_SENT:
				Toast.makeText(getApplicationContext(), "Message sent!",
						Toast.LENGTH_LONG).show();
				break;
			}
		}
	};

	@Override
	public NdefMessage createNdefMessage(NfcEvent event) {
		UserFunctions uf = new UserFunctions();
		
		NdefMessage message = new NdefMessage(NdefRecord.createMime(
				"application/com.gmics.unifiq", String.valueOf(Globals.id).getBytes())
		);
		return message;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.nfc:
			Intent intent = new Intent(Settings.ACTION_NFCSHARING_SETTINGS);
			startActivity(intent);
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}
	}

    @Override
    public void onResume() {
        super.onResume();
        // Check to see that the Activity started due to an Android Beam
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent().getAction())) {
            processIntent(getIntent());
        }
    }

    @Override
    public void onNewIntent(Intent intent) {
        // onResume gets called after this to handle the intent
        setIntent(intent);
    }

    /**
     * Parses the NDEF Message from the intent and prints to the TextView
     */
    void processIntent(Intent intent) {
        Parcelable[] rawMsgs = intent.getParcelableArrayExtra(
                NfcAdapter.EXTRA_NDEF_MESSAGES);
        // only one message sent during the beam
        NdefMessage msg = (NdefMessage) rawMsgs[0];
        uid = new String(msg.getRecords()[0].getPayload());

		Bundle bundle = new Bundle();
		bundle.putInt("userID", Integer.parseInt(uid));
		Intent in = new Intent(this, MemberDetail.class);
		in.putExtras(bundle);
		startActivity(in);
		finish();
        //new getUserDetails().execute();
        // record 0 contains the MIME type, record 1 is the AAR, if present
        //friendName.setText(new String(msg.getRecords()[0].getPayload()));
    }

}
